package com.imti.xmldemo;

import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;

public class MainActivity extends AppCompatActivity
{

    private static final String TAG = MainActivity.class.getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        initView();

        try
        {
            List<Person> resultList =
                XMLUtil.parsePersonListByPull(getAssets().open("person_list.xml"));

            for (Person person : resultList)
            {
                Log.d(TAG,
                    "id " + person.getId() +
                        " 姓名：" + person.getName() +
                        " 年龄：" + person.getAge());
            }

        } catch (XmlPullParserException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private void initView()
    {
        try
        {
            //获取xml数据流
            InputStream is = getAssets().open("person_list.xml");

            //创建SAX解析工具类
            MyHandler handler = new MyHandler();
            //获取SAX解析器
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            //开始解析
            parser.parse(is, handler);

            is.close();

            List<Person> result = handler.getmPersonList();

            for (Person person : result)
            {
                Log.d(TAG,
                    "id " + person.getId() +
                        " 姓名：" + person.getName() +
                        " 年龄：" + person.getAge());
            }

        } catch (IOException | SAXException | ParserConfigurationException e)
        {
            e.printStackTrace();
        }
    }
}